﻿using JiveMessenger.Utilities;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JiveMessenger.CustomControls
{
    /// <summary>
    /// Interaction logic for LoginControl.xaml
    /// </summary>
    public partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (ProgramSettings.Instance.HasTouchInput())
            {
                this.bnLogin.Width = 120;
                this.bnLogin.Height = 80;
            }
        }
        void passwordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            ((ViewModelLoginControl)this.DataContext).Password = passwordBox.SecurePassword;
            if (passwordBox.Password.Length == 0)
                passwordBox.Background = (VisualBrush)App.Current.Resources["PasswordCueBannerBrush"];// passwordBox.Background.Opacity = 1;
        }

        private void passwordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            passwordBox.Background = Brushes.Transparent;
            passwordBox.SelectAll();
        }

        private void JiveLoginControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.NewValue.GetType() == typeof(ViewModelLoginControl))
            {
                this.passwordBox.Password = StaticMethods.ToInsecureString(((ViewModelLoginControl)this.DataContext).Password);
                if(this.passwordBox.Password.Length>0)
                    passwordBox.Background = Brushes.Transparent;
            }
        }

        private void bnLogin_Click(object sender, RoutedEventArgs e)
        {
            ((Button)sender).Focus();
        }

        private void txtBoxUsername_GotFocus(object sender, RoutedEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void txtBoxAddress_GotFocus(object sender, RoutedEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }
    }
}
